package com.systemsjr.jrbbf.debts;

import java.util.Date;
import java.util.Vector;

import net.rim.device.api.database.Cursor;
import net.rim.device.api.database.DataTypeException;
import net.rim.device.api.database.Database;
import net.rim.device.api.database.DatabaseException;
import net.rim.device.api.database.Row;
import net.rim.device.api.database.Statement;

import com.systemsjr.jrbbf.account.Account;
import com.systemsjr.jrbbf.account.ManageAccount;
import com.systemsjr.jrbbf.common.FinanceEntity;
import com.systemsjr.jrbbf.db.SQLManager;

public class ManageDebts extends SQLManager {

	public ManageDebts(Database db) {
		super(db);
		// TODO Auto-generated constructor stub
	}
	
	public DebtsCredits add(DebtsCredits entity){

		try {
			Statement statement = _db
					.createStatement("INSERT INTO DebtsCredits VALUES(null, ?, ?, ?, ?, ?)");
			statement.prepare();
			statement.bind(1, entity.getName());
			statement.bind(2, entity.getAmount());
			statement.bind(3, entity.getDate().getTime());
			statement.bind(4, entity.getType());
			statement.bind(5, entity.getAccount().getId());
			statement.execute();
			statement.close();

			// Retrieve the auto-generated ID of the item just added
			entity.setId(_db.lastInsertedRowID());
			
		} catch (DatabaseException dbe) {
			// SQLiteDemo.errorDialog(dbe.toString());
		}

		return entity;
	}
	
	public void update(DebtsCredits entity){
		try {
			// Update the record in the DirectoryItems table for the given id
			Statement statement = _db
					.createStatement("UPDATE DebtsCredits SET name = ?, amount = ?, date = ?, type = ?, fkAccount = ? WHERE id = ?");
			statement.prepare();
			statement.bind(1, entity.getName());
			statement.bind(2, entity.getAmount());
			statement.bind(3, entity.getDate().getTime());
			statement.bind(4, entity.getType());
			statement.bind(5, entity.getAccount().getId());
			statement.bind(6, entity.getId());
			statement.execute();
			statement.close();
		} catch (DatabaseException dbe) {
			// SQLiteDemo.errorDialog(dbe.toString());
		}
	}

	public void deletes(FinanceEntity entity){
		try {
			Statement statement = _db
					.createStatement("DELETE FROM DebtsCredits WHERE id = ?");
			statement.prepare();
			statement.bind(1, entity.getId());
			statement.execute();
			statement.close();
		} catch (DatabaseException dbe) {
			// SQLiteDemo.errorDialog(dbe.toString());
		}
	}
	
	public void deleteAccount(Account entity){
		try {
			Statement statement = _db
					.createStatement("DELETE FROM DebtsCredits WHERE fkAccount = ?");
			statement.prepare();
			statement.bind(1, entity.getId());
			statement.execute();
			statement.close();
		} catch (DatabaseException dbe) {
			// SQLiteDemo.errorDialog(dbe.toString());
		}
	}
	
	public Vector getAll(){
		Vector debts = new Vector();

		try {
			// Read in all records from the DirectoryItems table
			Statement statement = _db
					.createStatement("SELECT id, name, amount, date, type, fkAccount FROM DebtsCredits");
			statement.prepare();
			Cursor cursor = statement.getCursor();
			ManageAccount accountManager = new ManageAccount(_db);
			// Iterate through the the result set. For each row, add a
			// new DirectoryItem object to the vector.
			while (cursor.next()) {
				DebtsCredits debt = new DebtsCredits();
				Row row = cursor.getRow();
				
				debt.setId(row.getInteger(0));
				debt.setAccount(accountManager.get(row.getInteger(5)));
				debt.setName(row.getString(1));
				debt.setAmount(row.getDouble(2));
				debt.setDate(new Date(row.getLong(3)));
				debt.setType(row.getString(4));
				
				debts.addElement(debt);
			}
			statement.close();
			cursor.close();
		} catch (DatabaseException dbe) {
			// SQLiteDemo.errorDialog(dbe.toString());
		} catch (DataTypeException dte) {
			// SQLiteDemo.errorDialog(dte.toString());
		}

		return debts;
	}
	
	public DebtsCredits get(long id){
		DebtsCredits debt = null;
		try {
			Statement statement = _db
					.createStatement("SELECT id, name, amount, date, type, fkAccount FROM DebtsCredits where id = ?");
			statement.prepare();
			statement.bind(1, id);
			Cursor cursor = statement.getCursor();
			ManageAccount accountManager = new ManageAccount(_db);
			if (cursor.next()) {
				Row row = cursor.getRow();
				debt = new DebtsCredits();
				debt.setId(row.getInteger(0));
				debt.setAccount(accountManager.get(row.getInteger(5)));
				debt.setName(row.getString(1));
				debt.setAmount(row.getDouble(2));
				debt.setDate(new Date(row.getLong(3)));
				debt.setType(row.getString(4));

			}
		} catch (DatabaseException dbe) {
			dbe.printStackTrace();
		} catch (DataTypeException dte) {
			dte.printStackTrace();
		}

		return debt;
	}

	public void delete(FinanceEntity entity) {
		// TODO Auto-generated method stub
		try {
			Statement statement = _db
					.createStatement("DELETE FROM DebtsCredits WHERE id = ?");
			statement.prepare();
			statement.bind(1, entity.getId());
			statement.execute();
			statement.close();
		} catch (DatabaseException dbe) {
			// SQLiteDemo.errorDialog(dbe.toString());
		}
	}
}
